//
//  DynamicCellConstraintFactory.m
//  mcrmSAAS
//
//  Created by wjxfhxy on 15-3-14.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "DynamicCellConstraintFactory.h"

@implementation DynamicCellConstraintFactory

-(XMSQueueDictionary*)factory:(ExpandInfo*)expandInfo originConstraint:(XMSQueueDictionary*)tmp
{
    XMSQueueDictionary *qd = nil;
    
    if(expandInfo.fieldDescript.count <= 0)
    {
        qd = tmp;
    }
    else
    {
        qd = [XMSQueueDictionary new];
        
        for(NSUInteger i = 0 ; i < expandInfo.fieldDescript.count; i++)
        {
            FieldDescript * fix = expandInfo.fieldDescript[i];
            
            NSMutableArray *array = [qd getValueForKey:fix.lineName];
            if(array == nil)
            {
                array = [NSMutableArray new];
                [qd addKey:fix.lineName value:array];
            }
            
            if(fix.fieldType.intValue == DynamicFieldTypeSystem || fix.fieldType.intValue == DynamicFieldTypeDefault )
            {
                [tmp enumerateArrayValueUsingBlock:^(id key, id obj, NSUInteger idx, BOOL *stop)
                 {
                     FieldDescript *d = obj;
                     if([d.fieldName isEqualToString:fix.fieldName])
                     {
                         d.optIsVisible = fix.optIsVisible;
                         d.viewIsVisible = fix.viewIsVisible;
                         d.isAllowEmpty = fix.isAllowEmpty;
                         d.fieldLength = fix.fieldLength;
                         
                         //系统或者默认字段 以服务返回的标签名称为准
                         d.fieldLabel = fix.fieldLabel;
                         
                         [array addObject:d];
                         
                         [(NSMutableArray*)[tmp getValueForKey:key] removeObject:d];
                         
                         *stop = YES;
                     }
                 }];
            }
            else
            {
                [array addObject:fix];
            }
        }
    }
    
    return qd;
}

-(XMSQueueDictionary*)constraint:(NSArray*)fieldDescripts
{
    XMSQueueDictionary *qd = [XMSQueueDictionary new];
    
    if(fieldDescripts.count <= 0)
    {

    }
    else
    {
        qd = [XMSQueueDictionary new];
        
        for(NSUInteger i = 0 ; i < fieldDescripts.count; i++)
        {
            FieldDescript * fix = fieldDescripts[i];
            
            NSMutableArray *array = [qd getValueForKey:fix.lineName];
            if(array == nil)
            {
                array = [NSMutableArray new];
                [qd addKey:fix.lineName value:array];
            }
            
            [array addObject:fix];
        }
    }
    
    return qd;
}

-(XMSQueueDictionary*)factoryForModify:(ExpandInfo*)expandInfo originConstraint:(XMSQueueDictionary*)tmp
{
    XMSQueueDictionary *qdSet = [self factory:expandInfo originConstraint:tmp];
    
    for(NSUInteger i = 0; i< qdSet.count ;)
    {
        XMSKvPair *pair = [qdSet kvPairAtIndex:i];
        NSMutableArray *array = pair.idValue;
        for(NSUInteger j = 0 ; j < array.count;)
        {
            FieldDescript *d = array[j];
            if(d.optIsVisible.intValue != 1)
            {
                [array removeObjectAtIndex:j];
                continue;
            }
            j++;
        }
        if(array.count <= 0)
        {
            [qdSet removeObjectAtIndex:i];
            continue;
        }
        i++;
    }
    
    return qdSet;
}

-(XMSQueueDictionary*)constraintForModify:(NSArray*)fieldDescripts
{
    XMSQueueDictionary *qdSet =  [self constraint:fieldDescripts];
    
    for(NSUInteger i = 0; i< qdSet.count ;)
    {
        XMSKvPair *pair = [qdSet kvPairAtIndex:i];
        NSMutableArray *array = pair.idValue;
        for(NSUInteger j = 0 ; j < array.count;)
        {
            FieldDescript *d = array[j];
            if(d.optIsVisible.intValue != 1)
            {
                [array removeObjectAtIndex:j];
                continue;
            }
            j++;
        }
        if(array.count <= 0)
        {
            [qdSet removeObjectAtIndex:i];
            continue;
        }
        i++;
    }
    
    return qdSet;
}

-(XMSQueueDictionary*)factoryForReview:(ExpandInfo*)expandInfo originConstraint:(XMSQueueDictionary*)tmp
{
    XMSQueueDictionary *qdSet = [self factory:expandInfo originConstraint:tmp];
    
    for(NSUInteger i = 0; i< qdSet.count ;)
    {
        XMSKvPair *pair = [qdSet kvPairAtIndex:i];
        NSMutableArray *array = pair.idValue;
        for(NSUInteger j = 0 ; j < array.count;)
        {
            FieldDescript *d = array[j];
            if(d.viewIsVisible.intValue != 1)
            {
                [array removeObjectAtIndex:j];
                continue;
            }
            
            if(d.controlType.intValue == DynamicFieldControlTypeSingleSelect)
                d.controlType.intValue = DynamicFieldControlTypeTextSingleSelect;
            else if(d.controlType.intValue == DynamicFieldControlTypeMultiSelect)
                d.controlType.intValue = DynamicFieldControlTypeMultiSelectStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeDateTime)
                d.controlType.intValue = DynamicFieldControlTypeDateTimeStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypePosition)
                d.controlType.intValue = DynamicFieldControlTypePositionStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeImagePanel)
                d.controlType.intValue = DynamicFieldControlTypeStaticImagePanel;
            
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateUUID)
                d.controlType.intValue = DynamicFieldControlTypeCreateUUIDStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateUserID)
                d.controlType.intValue = DynamicFieldControlTypeCreateUserIDStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateUserName)
                d.controlType.intValue = DynamicFieldControlTypeCreateUserNameStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateDeptID)
                d.controlType.intValue = DynamicFieldControlTypeCreateDeptIDStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateDeptName)
                d.controlType.intValue = DynamicFieldControlTypeCreateDeptNameStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeCreateUUID)
                d.controlType.intValue = DynamicFieldControlTypeCreateDateStaticText;
            
            if(d.controlType.intValue == DynamicFieldControlTypeSingleSelectDic)
                d.controlType.intValue = DynamicFieldControlTypeStaticSingleSelectDic;
            
            else
                d.controlType.intValue = DynamicFieldControlTypeStaticText;
            
            j++;
        }
        if(array.count <= 0)
        {
            [qdSet removeObjectAtIndex:i];
            continue;
        }
        i++;
    }
    
    return qdSet;
}

-(XMSQueueDictionary*)constraintForReview:(NSArray*)fieldDescripts
{
    XMSQueueDictionary *qdSet = [self constraint:fieldDescripts];
    
    for(NSUInteger i = 0; i< qdSet.count ;)
    {
        XMSKvPair *pair = [qdSet kvPairAtIndex:i];
        NSMutableArray *array = pair.idValue;
        for(NSUInteger j = 0 ; j < array.count;)
        {
            FieldDescript *d = array[j];
            if(d.viewIsVisible.intValue != 1)
            {
                [array removeObjectAtIndex:j];
                continue;
            }
            
            if(d.controlType.intValue == DynamicFieldControlTypeSingleSelect)
                d.controlType.intValue = DynamicFieldControlTypeTextSingleSelect;
            else if(d.controlType.intValue == DynamicFieldControlTypeMultiSelect)
                d.controlType.intValue = DynamicFieldControlTypeMultiSelectStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeDateTime)
                d.controlType.intValue = DynamicFieldControlTypeDateTimeStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypePosition)
                d.controlType.intValue = DynamicFieldControlTypePositionStaticText;
            else if(d.controlType.intValue == DynamicFieldControlTypeImagePanel)
                d.controlType.intValue = DynamicFieldControlTypeStaticImagePanel;
            
            else if(d.controlType.intValue == DynamicFieldControlTypeSingleSelectDic)
                d.controlType.intValue = DynamicFieldControlTypeStaticSingleSelectDic;
            else
                d.controlType.intValue = DynamicFieldControlTypeStaticText;
            
            j++;
        }
        if(array.count <= 0)
        {
            [qdSet removeObjectAtIndex:i];
            continue;
        }
        i++;
    }
    
    return qdSet;
}

@end
